package com.shu.mmall.controller.backend;

import com.shu.mmall.common.Const;
import com.shu.mmall.common.Result;
import com.shu.mmall.pojo.User;
import com.shu.mmall.service.IUserService;
import com.shu.mmall.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author shujinhao
 */
@RequestMapping("/user/manager")
@Controller
public class UserManagerController {
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public Result<User> login(String username, String password, HttpSession session){
        Result<User> result = userService.login(username, password);
        if (result.isSuccess()){
            User user=result.getData();
            if (user.getRole()==Const.Role.ROLE_ADMIN){
                session.setAttribute(Const.CURRENT_USER,user);
            }else {
                return Result.createByErrorMessage("不是管理员无法登录");
            }
        }
        return result;
    }
}
